/*
 * @date 2020年04月06日 18:18
 */
package org.jsola.hr.service.appro;

import org.jsola.approval.service.IApproListenerService;
import org.jsola.approval.vo.ApproModuleContext;
import org.jsola.hr.exception.HrException;

/**
 * 抽象审批监听
 * @author june
 */
public abstract class AbstractApproListener implements IApproListenerService {

    /**
     * 通过审批
     * @param approModuleContext 上下文
     * @return 上下文
     */
    public abstract ApproModuleContext passAppro(ApproModuleContext approModuleContext);

    /**
     * 驳回
     * @param approModuleContext 上下文
     * @return 上下文
     */
    public abstract ApproModuleContext rejectAppro(ApproModuleContext approModuleContext);

    /**
     * 撤回
     * @param approModuleContext 上下文
     * @return 上下文
     */
    public abstract ApproModuleContext revokeAppro(ApproModuleContext approModuleContext);

    /**
     * SubjectType
     * @return SubjectType
     */
    public abstract String getSubjectType();

    /**
     * 审批类型
     * @return 审批类型
     * @param approModuleContext 上下文
     */
    public abstract String getCode(ApproModuleContext approModuleContext);

    @Override
    public ApproModuleContext pass(ApproModuleContext approModuleContext) {
        String code = approModuleContext.getUserAppro().getApproTypeCode();
        HrException.hasText(code, "审批类型不能为空");
        if (code.equals(getCode(approModuleContext))) {
            return passAppro(approModuleContext);
        }
        return approModuleContext;
    }

    @Override
    public ApproModuleContext reject(ApproModuleContext approModuleContext) {
        String code = approModuleContext.getUserAppro().getApproTypeCode();
        HrException.hasText(code, "审批类型不能为空");
        if (code.equals(getCode(approModuleContext))) {
            return rejectAppro(approModuleContext);
        }
        return approModuleContext;
    }

    @Override
    public ApproModuleContext revoke(ApproModuleContext approModuleContext) {
        String code = approModuleContext.getUserAppro().getApproTypeCode();
        HrException.hasText(code, "审批类型不能为空");
        if (code.equals(getCode(approModuleContext))) {
            return revokeAppro(approModuleContext);
        }
        return approModuleContext;
    }

    @Override
    public Boolean isSupport(String subjectType, String subjectId) {
        return getSubjectType().equals(subjectType);
    }


}
